#controller.py
from model import TimeModel
from view import MainWindow, SubWindow
from PySide6.QtWidgets import QApplication
import sys
from threading import Thread, Event
import time

class Controller:
    def __init__(self):
        self.model = TimeModel()
        self.main_view = MainWindow(self)
        self.sub_view = None

    def open_sub_window(self, event):
        if not self.sub_view:
            self.sub_view = SubWindow(self)
            self.model.start()
            self.model._stop_event.clear()
            self.model._thread = Thread(target=self.update_time)
            self.model._thread.start()
        self.sub_view.show()

    def toggle_pause(self):
        if self.model._stop_event.is_set():
            self.model.start()
            self.sub_view.pause_button.setText("暂停")
        else:
            self.model.stop()
            self.sub_view.pause_button.setText("开始")

    def update_time(self):
        while not self.model._stop_event.is_set():
            current_time = self.model.get_time()
            if self.sub_view:
                self.sub_view.update_time(current_time)
            time.sleep(1)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    controller = Controller()
    controller.main_view.show()
    sys.exit(app.exec())